package com.pimee.web.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysDictType;
import com.pimee.model.web.Ztree;
import com.pimee.service.admin.ISysDictTypeService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "admin/sys/dict/type")
public class SysDictTypeController extends AbstractController {
	private String prefix = "admin/sys/dict/type";

	@Autowired
	private ISysDictTypeService sysDictTypeService;

	@ApiOperation("查询数据字典类型列表")
	@PostMapping("/list")
	@RequiresPermissions("system:dict:list")
	@ResponseBody
	public Object pageList(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysDictType> pageList = sysDictTypeService.pageList(params);
		return setSuccessModelMap(modelMap, pageList);
	}

	@ApiOperation("进入新增数据字典类型页")
	@GetMapping("/toAdd")
	public String toAdd(ModelMap modelMap) {
		return prefix + "/add";
	}

	@ApiOperation("保存新增数据字典类型")
	@RequiresPermissions("system:dict:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addSave(@Validated SysDictType param, ModelMap modelMap) {
		String loginName = ShiroUtils.getLoginName();
		param.setCreateBy(loginName);
		param.setUpdateBy(loginName);
		sysDictTypeService.insertDictType(param);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("进入修改字典类型页")
	@GetMapping("/toEdit/{dictId}")
	public String edit(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
		modelMap.put("dict", sysDictTypeService.selectByKey(dictId));
		return prefix + "/edit";
	}

	@ApiOperation("保存修改字典类型")
	@RequiresPermissions("system:dict:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editSave(@Validated SysDictType dict, ModelMap modelMap) {
		dict.setUpdateBy(ShiroUtils.getLoginName());
		sysDictTypeService.updateDictType(dict);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("查询字典详细")
	@RequiresPermissions("system:dict:list")
	@GetMapping("/detail/{dictId}")
	public String detail(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
		modelMap.put("dict", sysDictTypeService.selectByKey(dictId));
		modelMap.put("dictList", sysDictTypeService.listAll());
		return "admin/sys/dict/data/data";
	}

	@ApiOperation("删除数据字典类型")
	@RequiresPermissions("system:dict:remove")
	@PostMapping("/remove/{dictId}")
	@ResponseBody
	public Object remove(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
		sysDictTypeService.deleteByKey(dictId);
		return this.setSuccessModelMap(modelMap);
	}

	@ApiOperation("选择字典树")
	@GetMapping("/selectDictTree/{columnId}/{dictType}")
	public String selectDeptTree(@PathVariable("columnId") Long columnId, @PathVariable("dictType") String dictType,
			ModelMap mmap) {
		mmap.put("columnId", columnId);
		mmap.put("dict", sysDictTypeService.selectDictTypeByType(dictType));
		return prefix + "/tree";
	}

	@ApiOperation("加载字典列表树")
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData() {
		List<Ztree> ztrees = sysDictTypeService.selectDictTree();
		return ztrees;
	}
}
